home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8328 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  7.2 KB

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. Date: 3 Mar 1996 00:06:51 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Message-ID: <4hbctr$r7r@solutions.solon.com>
  8. References: <4gqpa1$3h9@alcor.usc.edu> <4hagqg$6je@daily-planet.execpc.com>
  9. NNTP-Posting-Host: solutions.solon.com
  10.  
  11. In article <4hagqg$6je@daily-planet.execpc.com>,
  12. Reginald W. Sprecher <sprecher@execpc.com> wrote:
  13. >1.  A pointer - a pointer is a variable which has as its purpose the
  14. >ability to point to another variable.  One thing that needs to be made
  15. >clear here is that the size of a pointer is always the same no matter
  16. >what type of data the pointer is pointing at ( a pointer to a char and
  17. >an int are the same size).
  18.  
  19. This is flatly untrue on at least one machine.
  20.  
  21. >2. address - all variables, including pointers and arrays, have a
  22. >address as to where they live in memory.
  23.  
  24. This is flatly untrue; register variables do not have an address in
  25. at least some cases, and a compiler may put a variable in a register,
  26. and thus deny it an address, if nothing ever uses its address.
  27.  
  28. >3. & - the & is the 'give me the address where the indicated variable
  29. >lives in memory' command.  All variables live in memory and therefore
  30. >have an address.  This address can only be resolved at runtime.
  31.  
  32. Once again, this is untrue.  The second part is also untrue; a clever
  33. compiler can easily determine the address of a variable in advance.
  34. (Especially on Unix, where each process gets its own address space.)
  35.  
  36. >void main(void)
  37.  
  38. This is illegal.
  39.  
  40. >{
  41. >  int a;
  42. >  int b[3];
  43. >  char c[4];
  44. >  char  *a_pointer;
  45.  
  46. >  a = sizeof(b);
  47. >  a = sizeof(c);
  48.  
  49. >  a_pointer = c;       // or a_pointer = &c[0]
  50.  
  51. This is a syntax error.
  52.  
  53. >  scanf("%s",c);
  54.  
  55. >}
  56.  
  57. >1.  The first four statement instructs our program to create memory
  58. >for the indicated variables on the stack.  After these instructions
  59. >are performed our memory looks like:
  60.  
  61. There is no guarantee that they are on the stack, although this is
  62. how it conventionally happens.  However, they are *not* statements;
  63. they are declarations.  The space is allocated before main begins
  64. executing.  (This makes little practical difference, but is important.
  65. In parcticular, there is no reason to assume that these four objects
  66. are stored in memory in any particular order.)
  67.  
  68.  
  69. >                       address     (our name)
  70. >________
  71. >|                |     1000        a  (because a is an int we need 2 bytes to store it)
  72.  
  73. Please keep lines to less than 80 characters.
  74.  
  75. Do not assume that ints are 2 bytes; it's often wrong.
  76.  
  77. >2.  The next thing we encounter is the sizeof instructions.  Be
  78. >carefull here, sizeof looks like a C runtime function, acts like a C
  79. >runtime function but is not a C runtime function.  It is really some
  80. >instructions to tell the compiler to calculate the size of the
  81. >indicated variable at compile time and not at runtime.  It is because
  82. >sizeof is performed by the compiler that the sizeof an array (or any
  83. >variable) can be determined.
  84.  
  85. Nothing prevents sizeof from being done at runtime in a C interpreter;
  86. it just wouldn't be *useful*.
  87.  
  88. >3.  After the sizeof are done memory looks like..
  89.  
  90. The rest of the examples all assume byte order within an int, which is
  91. also stupid.
  92.  
  93. >Is the name of an array a pointer?  The answer to this, using our
  94. >definition of what a pointer is, is no.  The name of an array is not a
  95. >pointer, but it is what we would call an address.  This is because we
  96. >define all arrays in terms of where they begin in memory.  Therefore
  97. >the following expressions are equal
  98.  
  99. The name of an array is not an address, either; it is the name of an
  100. array.  If you look at how sizeof() works on it, it's clear that
  101. the array only generates a pointer when one is appropriate.
  102.  
  103. >This fact therefore makes it possible to intermix the following array
  104. >notation:
  105.  
  106. >c[1]  or *(c + 1)
  107.  
  108. No, what makes it possible to intermix those is that the former is *defined*
  109. to be the latter.
  110.  
  111. >*(array_name + (index * size_of_data_in_array))
  112.  
  113. Except that this is wrong; it's
  114.     *(array_name + index)
  115. you meant
  116.     *( ((char *) array_name) + (index * sizeof(array_name[0])) )
  117.  
  118. >4.  Scanf is a function that says it wants 'a pointer' to a variable.
  119. >What it really wants is the address of the variable where it (scanf)
  120. >should put the data it has.  That is why the following scanf
  121. >expressions will put the data in the same place:
  122.  
  123. >scanf("%s",c);    AND  scanf("%s",a_pointer);
  124.  
  125. Amazing!  This one is basically correct.  (Except that scanf sometimes
  126. wants other types, some of which are not pointers.)
  127.  
  128. >1)  Using the name of an array somehow yields or calculates or
  129. >generates the address.
  130.  
  131. >This is not true. The name of the array is the address of the array.
  132. >The operation is not a calculation or de-referencing, or ....
  133. >I have personally never heard of this 'decay' process.  If you look
  134. >what is really happening inside the code there is no decay process.
  135. >This is because the program always reference an array by its address
  136. >which is the address of the first element in the array.  This same
  137. >pricincipal applies to multi-dimensional arrays as well.
  138.  
  139. This is entirely bullshit.  The array name, when used in the right
  140. sort of contexts (mostly, "outside of sizeof"), is removed, and replaced
  141. by a pointer to its first element.
  142.  
  143. It turns into the address of *the first element*, not the address of the
  144. array.  This is different; the address of the array is of type
  145. (pointer to array[N] of T) - the address of the first element is
  146. of type (pointer to T).
  147.  
  148. >2)  &c is a pointer to something.
  149.  
  150. >&c is by the above definition is not a pointer, it is an address, and
  151. >specifically it is an address to a variable called c, which is of type
  152. >char, and whose first element is at some address.
  153.  
  154. Debateable, but it works the same way; &c can be passed to a function
  155. expecting (pointer to type-of-c).
  156.  
  157. >&c is the address of myarray which is:
  158.  
  159. >&c == c == &c[0] == 1008 (in our memory map)
  160.  
  161. But it is of a different type than the others.
  162.  
  163. >3)  Pointer take on different sizes.
  164.  
  165. >Pointers only take on a different size when going from one platform to
  166. >another.  If you write a program and define 1000 pointers that can
  167. >point at base data types or your own structures, the size (amount of
  168. >memory) that the pointer varialbe it self consumes is still the same
  169. >size.  Again, the factor here is what is the address space of your
  170. >target system.
  171.  
  172. This is bullshit, pure and simple.  sizeof(void *) and sizeof(void (*)(void))
  173. are likely to be different.  The only cases where the sizes are guaranteed
  174. to remain the same are sizeof(void *) == sizeof(char *), and
  175. sizeof(T *) == sizeof(const T *) and the like.
  176.  
  177. On *most common platforms* it may be the case that pointers to different
  178. types are the same size and are represented the same way.  It is not
  179. always true.  My home computer has pointers which are 32 bit pointers
  180. by bytes, and it has pointers which are 30 bit pointers by longwords,
  181. and the top two bits are never used.  The latter are rarely used, but
  182. they're perfectly reasonable pointers.
  183.  
  184. -s
  185. -- 
  186. Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
  187. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  188. FUCK the communications decency act.  Goddamned government.  [literally.]
  189. The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
  190.